package com.lzh.walletdemo.service.impl;

import com.lzh.walletdemo.entity.LogAmount;
import com.lzh.walletdemo.mapper.UserOperationMapper;
import com.lzh.walletdemo.service.UserOperationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class UserOperationServiceImpl implements UserOperationService {
    @Autowired
    private UserOperationMapper userOperationMapper;
    private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    //表示金额减少
    private static final Integer DECREASE = 0;
    //表示金额增加
    private static final Integer INCREASE = 1;


    /**
     * 查询用户钱包余额
     * @param userId 用户id
     * @return 返回余额
     */
    @Override
    public BigDecimal getBalance(Integer userId) {
        BigDecimal balance = userOperationMapper.getBalance(userId);
        return balance;
    }

    /**
     * 用户支付数额
     * @param payMoney 花费钱数
     * @param userId 用户id
     * @return 消费成功/消费失败
     */
    @Transactional
    @Override
    public String pay(BigDecimal payMoney,Integer userId) {
        //封装参数
        Map<Object,Object> map = new HashMap<Object,Object>();
        map.put("payMoney",payMoney);
        map.put("userId",userId);
        //记录到日志中
        LogAmount logAmount = new LogAmount();
        //0表示花费金额
        logAmount.setChange_status(DECREASE);
        //当前日期
        logAmount.setLog_date(sdf.format(new Date()));
        //变动金额
        logAmount.setLog_amount_change(payMoney.toString());
        //用户id
        logAmount.setUser_id(userId);
        try {
            userOperationMapper.pay(map);
            userOperationMapper.recordLog(logAmount);
            return "消费成功";
        }catch(Exception e){
            System.out.println("异常信息:" + e.getMessage());
            return "消费失败";
        }
    }

    /**
     * 用户退费
     * @param backMoney 退费金额
     * @param userId 用户id
     * @return 退费成功/退费失败
     */
    @Transactional
    @Override
    public String balanceBack(BigDecimal backMoney,Integer userId) {
        Map<Object,Object> map = new HashMap<Object,Object>();
        map.put("backMoney",backMoney);
        map.put("userId",userId);
        //记录到日志中
        LogAmount logAmount = new LogAmount();
        //0表示花费金额
        logAmount.setChange_status(INCREASE);
        //当前日期
        logAmount.setLog_date(sdf.format(new Date()));
        //变动金额
        logAmount.setLog_amount_change(backMoney.toString());
        //用户id
        logAmount.setUser_id(userId);
        try {
            userOperationMapper.balanceBack(map);
            userOperationMapper.recordLog(logAmount);
            return "退款成功";
        }catch (Exception e){
            System.out.println(e.getMessage());
            return "退款失败";
        }
    }

    /**
     * 查询用户钱包金额变动情况
     * @param userId 用户id
     * @return 返回用户的所有金额变动情况
     */
    @Override
    public List<Map<String,Object>> getAmountLog(Integer userId) {

        List<LogAmount> list = userOperationMapper.getAmountLog(userId);
        List<Map<String,Object>> handleList = new ArrayList<Map<String,Object>>();
        //数据处理
        for (int i = 0; i < list.size(); i++) {
            Map<String,Object> map  = map = new HashMap<String,Object>();;

            map.put("log_id",list.get(i).getLog_id());
            map.put("log_date",list.get(i).getLog_date());
            map.put("user_real_name",list.get(i).getUserInfo().getUser_real_name());
            if (list.get(i).getChange_status().equals(1)){
                map.put("log_amount_change","+" + list.get(i).getLog_amount_change());
            }else if(list.get(i).getChange_status().equals(0)){
                map.put("log_amount_change","-" + list.get(i).getLog_amount_change());
            }
            handleList.add(map);
        }
        return handleList;
    }

}
